<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/5
     * Time: 下午2:43
     */

    namespace app\store\model;

    use app\common\utils\UtilTree;
    use think\Db;
    use think\Model;

    class CategoryModel extends Model
    {

        protected $name = 'store_category';

        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i','update_time' => 'timestamp:Y-m-d H:i'];

        /**
         * 获取商品大分类
         *
         * @return array
         */
        static public function getCategory()
        {

            return self::where([])->column('id,name');
        }

        /**
         * @param $fileds
         *
         * @return array
         */
        static public function getColumn($fileds,$default = [])
        {

            if (empty($default))
            {
                return self::where([])->column($fileds);
            }
            else
            {
                $column                = self::where([])->column($fileds);
                $column[key($default)] = $default[key($default)];
                return $column;
            }

        }

        /**
         * 获取层级列表
         *
         * @param string $filed
         * @param array  $where
         *
         * @return array
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function GetlayerList($filed = 'id,pid,name,img',$where = [])
        {

            return UtilTree::toLayer(self::field($filed)->where($where)->select());
        }

        /**
         * @param string $field
         * @param array  $condition
         *
         * @return array|false|\PDOStatement|string|\think\Collection
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function CategoryLayer($field = '*',$condition = [])
        {

            $condition['show'] = 1;
            $result            = self::field($field)->where($condition)->order('pid asc,sort asc,id asc')->select();
            foreach ($result as &$item)
            {
                $item['image'] = get_file_path($item['image']);
            }

            UtilTree::config([
                'id'    => 'id',    // id名称
                'pid'   => 'pid',   // pid名称
                'title' => 'name', // 标题名称

            ]);
            $result = UtilTree::toLayer($result);
            return $result;
        }

        /**
         * @param string $field
         * @param array  $condition
         *
         * @return array|false|\PDOStatement|string|\think\Collection
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function CategoryList($field = '*',$condition = [])
        {

            $result = CategoryModel::field($field)->where($condition)->order('pid asc,sort asc,id asc')->select();
            foreach ($result as &$item)
            {
                $item['image'] = get_file_path($item['image']);

            }
            unset($item);

            return $result;
        }

        /**
         * 获取所有子集ID
         *
         * @param $pid
         *
         * @return array
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function getChildsID($pid)
        {

            $StoreCategory    = self::field('id,pid')->select();
            $Category_ids_arr = UtilTree::getChilds($StoreCategory,$pid);
            $Category_ids     = [];
            foreach ($Category_ids_arr as $item)
            {
                $Category_ids[] = $item['id'];
            }
            array_push($Category_ids,$pid);
            return $Category_ids;
        }

    }